import Vue from 'vue'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element from 'element-ui'
import './styles/element-variables.scss'

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import './icons' // icon

import * as filters from './filters' // global filters
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { AjaxToken } from "@/api/common"
import VueSession from 'vue-session'
import sessionStorage from '@/api/Storage'
import infiniteScoll from 'vue-infinite-scroll'
import echarts from 'echarts'



Vue.prototype.$echarts = echarts;
Vue.use(infiniteScoll)
Vue.use(VueSession)
NProgress.configure({ showSpinner: false }) // NProgress Configuration
/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
import { mockXHR } from '../mock'
if (process.env.NODE_ENV === 'production') {
  mockXHR()
}

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false
if (sessionStorage.getItem('token') && sessionStorage.getItem('route')) {
  router.options.routes = []
  var routes = JSON.parse(sessionStorage.getItem('route'))
  routes.forEach(element => {
    router.options.routes.push(element)
  })
}
router.beforeEach((to, from, next) => {
  // 没登录则跳转到登录界面
  if ((to.path === '/login') || to.redirectedFrom === '/login' || sessionStorage.getItem('token')) {
     // console.log("权限开启");
    //权限
    var routes = localStorage.getItem('Role');
    //判断是否有权限
    if (routes!=null&&!routes.includes(to.path)) {
      next({
        path: "/404",
      });
    } else {
      next()
    
    }



  } else {
    AjaxToken(this, "IsLogin", [], "post", function (response) {
      var body = response.data;
      if (to.path == "/dashboard" || to.path == "/") {
        next({
          path: "/login",
        });
      } else {
        next({
          path: "/login",
          query: {
            msg: "登录已过期,请退出重新登录"
          }
        });
      }
    }, function (err) {
      if (to.path == "/dashboard" || to.path == "/") {
        next({
          path: "/login",
        });
      } else {
        next({
          path: "/login",
          query: {
            msg: "登录已过期,请退出重新登录"
          }
        });
      }
    });
  }
})
router.afterEach(() => {
  // finish progress bar
  // eslint-disable-next-line no-undef
  NProgress.done()
})
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
